package com.bjqg.bjqg01.bjqg.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.adapter.GridViewAdapter;
import com.yzq.zxinglibrary.android.CaptureActivity;

/**
 * Created by bjqg01 on 2017/12/11.
 */
public class HomeActivity extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private int REQUEST_CODE_SCAN = 111;
    private ImageView setting;
    private TextView nowCity;
    private TextView nowTime;
    private GridView homeGrid;
    private String text[] = {
            "公告", "价格查询",
            "取样方法", "网上委托",
            "网上支付", "报告查询",
            "联系方式", "报告识别",
            "诚信评价", "在线留言"};
    private int img_grid[] = {R.mipmap.gg, R.mipmap.jgcx, R.mipmap.qyff,
            R.mipmap.wswt, R.mipmap.pay, R.mipmap.bgcx, R.mipmap.lxfs, R.mipmap.bgsb, R.mipmap.cxpj, R.mipmap.zxly};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_internet_palm);

        initView();


    }

    private void initView() {
        setting = (ImageView) findViewById(R.id.setting);
        nowCity = (TextView) findViewById(R.id.tv_hs);
        nowTime = (TextView) findViewById(R.id.tv_day);
        homeGrid = (GridView) findViewById(R.id.gridView);


        setting.setOnClickListener(this);
        onBindGrid();
    }

    private void onBindGrid() {
        homeGrid.setAdapter(new GridViewAdapter(this, text, img_grid));
        homeGrid.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (text[position]){
            case "公告":
                Intent intentNotice = new Intent();
                intentNotice.setClass(this, NoticeActivity.class);
//                intentNotice.putExtra("NoticeActivity", text[position]);
                startActivity(intentNotice);
                break;
            case "价格查询":
                Intent intentPriceInquiry = new Intent();
                intentPriceInquiry.setClass(this, PriceInquiryActivity.class);
//                intentPriceInquiry.putExtra("PriceInquiryActivity", text[position]);
                startActivity(intentPriceInquiry);
                break;
            case "取样方法":
                Intent intentSamplingMethod = new Intent();
                intentSamplingMethod.setClass(this, SampleMethodActivity.class);
//                intentSamplingMethod.putExtra("SamplingMethodActivity", text[position]);
                startActivity(intentSamplingMethod);
                break;
            case "网上委托":
                Intent intentEntrus = new Intent();
                intentEntrus.setClass(this, OnLineEntrustActivity.class);
//                intentEntrus.putExtra("OnLineEntrustActibity", text[position]);
                startActivity(intentEntrus);
                break;
            case "网上支付":
                Intent intentPay = new Intent();
                intentPay.setClass(this, EntrusManageActibity.class);
//                intentPay.putExtra("OnLinePayActibity", text[position]);
                startActivity(intentPay);
                break;
            case "报告识别":
                if (Build.VERSION.SDK_INT>22){
                    if (ContextCompat.checkSelfPermission(this,
                            android.Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
                        //先判断有没有权限 ，没有就在这里进行权限的申请
                        ActivityCompat.requestPermissions(this,
                                new String[]{android.Manifest.permission.CAMERA},1);

                    }else {
//                        说明已经获取到摄像头权限了 想干嘛干嘛
                        Intent intent = new Intent(this, CaptureActivity.class);
//                        intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
                        startActivityForResult(intent, REQUEST_CODE_SCAN);
                    }
                }else {

//                    这个说明系统版本在6.0之下，不需要动态获取权限。
                    Intent intent = new Intent(this, CaptureActivity.class);
//                    intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
                    startActivityForResult(intent, REQUEST_CODE_SCAN);

                }

                break;
            case "报告查询":
                Intent intentRecognitionFind = new Intent();
                intentRecognitionFind.setClass(this, ReportFindActivity.class);
//                intentRecognitionFind.putExtra("RecognitionFindActivity", text[position]);
                startActivity(intentRecognitionFind);
                break;
            case "在线留言":
                Intent intentOnlineMessage = new Intent();
                intentOnlineMessage.setClass(this, OnlineMessageActivity.class);
//                intentOnlineMessage.putExtra("OnlineMessageActivity", text[position]);
                startActivity(intentOnlineMessage);
                break;
            case "联系方式":
                Log.i("联系方式", "联系方式");
                Intent intentContactInformation = new Intent();
                intentContactInformation.setClass(this, ContactInformationActivity.class);
//                intentContactInformation.putExtra("ContactInformationActivity", text[position]);
                startActivity(intentContactInformation);
                break;
            case "诚信评价":
                Toast.makeText(this, "此功能暂没开发", Toast.LENGTH_LONG).show();
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.setting:

                Intent intent = new Intent(getApplicationContext(), SettingActivity.class);
                startActivity(intent);
                break;
        }
    }
}
